package com.health.system.springSecurity;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import com.health.system.javacommon.util.*;
import com.health.system.springSecurity.UserInfo;

/**
 * 取得登录后的用户对象
 * 
 * @author 
 * 
 */
public class LogonUser {

	/**
	 * @return 登录用户对象(未登录则为null)
	 */
	public static UserInfo logonUser() {
		if (SecurityContextHolder.getContext().getAuthentication() != null) {
			Object obj = SecurityContextHolder.getContext().getAuthentication()
					.getPrincipal();
			if (obj instanceof UserInfo) {
				return (UserInfo) obj;
			} else {
				return null;  //原来是这个
				//return new UserInfo("admin", "123456", true,true, true,true, null);
			}
		}
		return null;
	}

	/**
	 * 取得用户ID
	 * 
	 * @author ZC 2011.3.23
	 * 
	 * @return 用户ID
	 */
	public static String getUserId() {
		String userId = null;
		UserInfo userInfo = logonUser();
		if (null != userInfo) {
			userId = userInfo.getUserId();
		}
		return userId;
	}
	
	/**
	 * 取得用户名
	 * 
	 * @author pch 2010.7.8
	 * @author bzq 2011.4.18 update
	 * @return 用户名
	 */
	public static String getUserName() {
		String userName = "";
		UserInfo userInfo = logonUser();
		if (null != userInfo) { 
			userName = (String)getUserNamex(userInfo.getUsername());
			System.out.println("####@!! userName="+userName);
		}
		return userName;
	}
	
	
	/**
	 * 取得用户真实用户名 
	
	 * @return 用户名
	 */
	public static String getUserRealName() {
		String userRealName = "";
		UserInfo userInfo = logonUser();
		if (null != userInfo) { 
			userRealName = (String)userInfo.getUserRealname();
			System.out.println("####@!! userRealName="+userRealName);
		}
		return userRealName;
	}
	
	
	/**
	 * 分割 前台传来的 登录名称
	 * 如果是登录名+,+机构代码则分割字符串，取第一节返回
	 * @param name
	 * @author bzq 2010.7.8
	 * @return
	 */
	private static String getUserNamex(String name){
		String[] temp = StringTool.SplitString(name, ",");
		String j_username ="";
		String j_organCode = "";
		if(null!=temp&&temp.length>=2){
			 j_username = temp[0];
			 j_organCode = temp[1];
			 return j_username;
		}else{
			return name;
		}
	}
	/**
	 * 取得用户cpid
	 * 
	 * @author pch 2010.7.8
	 * 
	 * @return 用户cpid
	 */
	public static String getUserCpid() {
		String userCpid = null;
		UserInfo userInfo = logonUser();
		if (null != userInfo) {
			userCpid = userInfo.getCpId();
		}
		return userCpid;
	}

	/**
	 * 取得用户cpcode
	 * 
	 * @author pch 2010.7.8
	 * 
	 * @return 用户cpcode
	 */
	public static String getUserCpcode() {
		String userCpcode = null;
		UserInfo userInfo = logonUser();
		if (null != userInfo) {
			userCpcode = userInfo.getCpCode();
		}
		return userCpcode;
	}

	/**
	 * 取得用户角色(','分割)
	 * 
	 * @author pch 2010.7.8
	 * 
	 * @return 用户角色(','分割)
	 */
	public static String getUserRoles() {
		String userRoles = null;
		UserInfo userInfo = logonUser();
		if (null != userInfo) {
			userRoles = userInfo.getRole();
		}
		return userRoles;
	}

	/**
	 * 取得用户角色中文名(','分割)
	 * 
	 * @author pch 2010.7.8
	 * 
	 * @return 用户角色中文名(','分割)
	 */
	public static String getUserRoleNames() {
		String userNames = null;
		UserInfo userInfo = logonUser();
		if (null != userInfo) {
			userNames = userInfo.getRoleName();
		}
		return userNames;
	}

	/**
	 * 取得用户所属公司中文名简称
	 * 
	 * @author pch 2010.7.8
	 * 
	 * @return 用户所属公司中文名简称
	 */
	public static String getUserCompanyName() {
		String userCompany = null;
		UserInfo userInfo = logonUser();
		if (null != userInfo) {
			userCompany = userInfo.getComName();
		}
		return userCompany;
	}

	/**
	 * 取得用户登录IP地址
	 * 
	 * @author pch 2010.7.8
	 * 
	 * @return 用户登录IP地址
	 */
	public static String getUserIp() {
		String userIp = null;
		UserInfo userInfo = logonUser();
		if (null != userInfo) {
			userIp = userInfo.getIp();
		}
		return userIp;
	}

}
